package cn.sciento.transfer.api.controller.v1;

import cn.sciento.core.domain.Page;
import cn.sciento.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import cn.sciento.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.sciento.boot.platform.lov.annotation.ProcessLovValue;
import cn.sciento.core.base.BaseController;
import cn.sciento.core.util.Results;
import cn.sciento.mybatis.domian.SecurityToken;
import cn.sciento.mybatis.helper.SecurityTokenHelper;
import cn.sciento.starter.keyencrypt.core.Encrypt;
import cn.sciento.transfer.api.dto.ProducerConfigDTO;
import cn.sciento.transfer.app.service.ProducerConfigService;
import cn.sciento.transfer.domain.entity.ProducerConfig;
import cn.sciento.transfer.domain.repository.ProducerConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = {"Producer  Config (Site Level)"})
@RestController("producerConfigSiteController.v1")
@RequestMapping({"/v1/producer-config"})
public class ProducerConfigSiteController extends BaseController {
  @Autowired
  private ProducerConfigRepository producerConfigRepository;
  
  @Autowired
  private ProducerConfigService producerConfigService;
  
  @ApiOperation("创建数据消息生产配置")
  @Permission(level = ResourceLevel.SITE)
  @PostMapping
  public ResponseEntity<ProducerConfig> createProducerConfig(@Encrypt @RequestBody ProducerConfig producerConfig) {
    this.producerConfigService.createProducerConfig(producerConfig);
    return Results.success(producerConfig);
  }
  
  @ApiOperation("更新数据消息生产配置")
  @Permission(level = ResourceLevel.SITE)
  @PutMapping
  public ResponseEntity<ProducerConfig> updateProducerConfig(@Encrypt @RequestBody ProducerConfig producerConfig) {
    validObject(producerConfig);
    this.producerConfigService.updateProducerConfig(producerConfig);
    return Results.success(producerConfig);
  }
  
  @ApiOperation("删除数据消息生产配置")
  @Permission(level = ResourceLevel.SITE)
  @DeleteMapping
  public ResponseEntity<Integer> deleteProducerConfig(@Encrypt @RequestBody ProducerConfig producerConfig) {
    return Results.success(this.producerConfigService.removeProducerConfig(producerConfig));
  }
  
  @ApiOperation("分页查询数据消息生产配置列表")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<Page<ProducerConfig>> listProducerConfig(@Encrypt ProducerConfigDTO producerConfig, @ApiIgnore @SortDefault(value = {"producerConfigId"}, direction = Sort.Direction.DESC) PageRequest pageRequest) {
    Page<ProducerConfig> list = this.producerConfigService.listProducerConfig(pageRequest, producerConfig);
    return Results.success(list);
  }
  
  @ApiOperation("详情查询数据消息生产配置列表")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping({"/{producerConfigId}"})
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<ProducerConfig> detailProducerConfig(@Encrypt @PathVariable("producerConfigId") Long producerConfigId) {
    ProducerConfig producerConfig = this.producerConfigRepository.selectProducerConfig(producerConfigId);
    return Results.success(producerConfig);
  }
  
  @ApiOperation("生成消息队列TOPIC")
  @Permission(level = ResourceLevel.SITE)
  @PostMapping({"/{producerConfigId}"})
  public ResponseEntity createProducerTopic(@Encrypt @PathVariable("producerConfigId") Long producerConfigId) {
    this.producerConfigService.reCreateTopic(producerConfigId);
    return Results.success();
  }
  
  @ApiOperation("刷新配置DDL语句")
  @Permission(level = ResourceLevel.SITE)
  @PutMapping({"/{producerConfigId}/refresh"})
  public ResponseEntity<ProducerConfig> refreshDDL(@Encrypt @PathVariable("producerConfigId") Long producerConfigId, @Encrypt @RequestBody ProducerConfig producerConfig) {
    producerConfig.setProducerConfigId(producerConfigId);
    SecurityTokenHelper.validToken((SecurityToken)producerConfig);
    this.producerConfigService.refreshDDL(producerConfig);
    return Results.success(producerConfig);
  }
}
